package cn.xorange.commons.security.token;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;
import java.util.Collections;

/**
 * @author : yangjian
 * @date : 2023/11/25
 * @description :
 */
public class CustomAuthToken extends AbstractAuthenticationToken {

    /** 登录名 */
    private final Object principal;

    public CustomAuthToken(Object principal, JSONObject object){
        super(Collections.emptyList());
        this.principal = principal;
        this.setDetails(object);
        this.setAuthenticated(false);
    }

    public CustomAuthToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        super.setAuthenticated(true);
    }




    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot setObject this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }
}
